package com.ssy.lingxi.member.merchant.entity;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.ssy.lingxi.member.merchant.handler.converter.JpaJsonToMapConverter;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 用户信息表
 * @author 万宁
 * @version 2.0.0
 * @date 2020-06-28
 */
@Entity
@Table(schema = "public", name = "ms_mc_member_user", indexes = {@Index(name="ms_mc_member_user_account_idx", columnList = "account"), @Index(name="ms_mc_member_user_country_code_idx", columnList = "countryCode"), @Index(name="ms_mc_member_user_phone_idx", columnList = "phone"), @Index(name="ms_mc_member_user_email_idx", columnList = "email"), @Index(name="ms_mc_member_user_member_id_idx", columnList = "member_id"), @Index(name="ms_mc_member_user_rel_type_idx", columnList = "relType"), @Index(name="ms_mc_member_user_type_enum_idx", columnList = "typeEnum"), @Index(name="ms_mc_member_user_is_sales_idx", columnList = "isSales")})
public class MemberUserDO implements Serializable {
    private static final long serialVersionUID = -1027435514346957959L;

    /**
     * 用户Id
     */
    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="member_user_seq")
    @SequenceGenerator(sequenceName="ms_mc_member_user_seq", name="member_user_seq",allocationSize = 1)
    private Long id;

    /**
     * 创建时间
     */
    @Column(columnDefinition = "timestamp")
    private LocalDateTime createTime;

    /**
     * 用户账号
     */
    @Column(columnDefinition = "varchar(50)", nullable = false)
    private String account;

    /**
     * 用户姓名
     */
    @Column(columnDefinition = "varchar(100)")
    private String name;

    /**
     * 用户真实姓名
     */
    @Column(columnDefinition = "varchar(100)")
    private String realName;

    /**
     * 手机号码前缀（国家代码）
     */
    @Column(columnDefinition = "varchar(20)")
    private String countryCode;

    /**
     * 手机号码
     */
    @Column(columnDefinition = "varchar(20)", nullable = false)
    private String phone;

    /**
     * 密码
     */
    @Column(columnDefinition = "varchar(50)", nullable = false)
    private String password;

    /**
     * 邮箱
     */
    @Column(columnDefinition = "varchar(100)", nullable = false)
    private String email;

    /**
     * 身份证号码
     */
    @Column(columnDefinition = "varchar(40)")
    private String idCardNo;

    /**
     * 身份证正反面图片 front-正面(人头像) back-反面(国徽)
     */
    @Convert(converter = JpaJsonToMapConverter.class)
    @Column(columnDefinition = "jsonb")
    private Map<String, Object> idCardImg;

    /**
     * 是否实名验证 1-未验证 2-已验证
     */
    @Column(columnDefinition = "int default 1")
    private Integer isAuth=1;

    /**
     * 职位
     */
    @Column(columnDefinition = "varchar(20)")
    private String jobTitle;

    /**
     * 用户头像
     */
    @Column(columnDefinition = "varchar(400)")
    private String logo;

    /**
     * 最后一次登录时间
     */
    @Column(columnDefinition = "timestamp")
    private LocalDateTime lastLoginTime;

    /**
     * 多对一双向关联会员
     */
    @JsonManagedReference
    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "member_id")
    private MemberDO member;

    /**
     * 多对一单向关联会员自定义的组织机构
     */
    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "org_id", referencedColumnName = "id")
    private MemberOrganizationDO org;

    /**
     * 关联角色
     */
    @JsonManagedReference
    @ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinTable(name="ms_mc_member_user_member_role_relation", joinColumns={ @JoinColumn(name="user_id", referencedColumnName="id")}, inverseJoinColumns={@JoinColumn(name="member_role_id", referencedColumnName="id")})
    private Set<MemberRoleDO> roles;

    /**
     * 关联类型，定义在 MemberRelationTypeEnum中：0-平台用户，1-其他用户
     */
    @Column
    private Integer relType;

    /**
     * 用户类型： 0-普通用户 1-会员超级管理员（会员新增或注册的时候指定，定义在MemberUserAuthTypeEnum）
     */
    @Column(columnDefinition = "int2")
    private Integer typeEnum;

    /**
     * 是否业务员，0-不是，1-是
     */
    @Column(columnDefinition = "int2")
    private Integer isSales;

    /**
     * 一对一双向关联会员权限
     */
    @JsonManagedReference
    @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, fetch = FetchType.LAZY)
    @JoinColumn(name="user_auth_id",referencedColumnName="id")
    private MemberUserAuthDO userAuth;

    /**
     * 用户状态 0-停用 1-启用
     */
    @Column(columnDefinition = "int2")
    private Integer status;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getIdCardNo() {
        return idCardNo;
    }

    public void setIdCardNo(String idCardNo) {
        this.idCardNo = idCardNo;
    }

    public String getJobTitle() {
        return jobTitle;
    }

    public void setJobTitle(String jobTitle) {
        this.jobTitle = jobTitle;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public LocalDateTime getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(LocalDateTime lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public MemberDO getMember() {
        return member;
    }

    public void setMember(MemberDO member) {
        this.member = member;
    }

    public MemberOrganizationDO getOrg() {
        return org;
    }

    public void setOrg(MemberOrganizationDO org) {
        this.org = org;
    }

    public Set<MemberRoleDO> getRoles() {
        return roles;
    }

    public void setRoles(Set<MemberRoleDO> roles) {
        this.roles = roles;
    }

    public Integer getRelType() {
        return relType;
    }

    public void setRelType(Integer relType) {
        this.relType = relType;
    }

    public Integer getTypeEnum() {
        return typeEnum;
    }

    public void setTypeEnum(Integer typeEnum) {
        this.typeEnum = typeEnum;
    }

    public Integer getIsSales() {
        return isSales;
    }

    public void setIsSales(Integer isSales) {
        this.isSales = isSales;
    }

    public MemberUserAuthDO getUserAuth() {
        return userAuth;
    }

    public void setUserAuth(MemberUserAuthDO userAuth) {
        this.userAuth = userAuth;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Map<String, Object> getIdCardImg() {
        return idCardImg;
    }

    public void setIdCardImg(Map<String, Object> idCardImg) {
        this.idCardImg = idCardImg;
    }

    public Integer getIsAuth() {
        return isAuth;
    }

    public void setIsAuth(Integer isAuth) {
        this.isAuth = isAuth;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        MemberUserDO that = (MemberUserDO) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
